package com.mp.approvalreq.servlet.redpoint;

import com.mp.approvalreq.entity.NewRedPoint;
import com.mp.approvalreq.service.RedPointServicePrx;
import com.mp.approvalreq.servlet.AbstractServlet;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.message.SuccessMsg;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;


/**
 * 获取某个栏目的红点
 */
@WebServlet("/getRedPointByType")
public class GetRedPointByType extends AbstractServlet {
	private static final Logger logger = LoggerFactory.getLogger(GetRedPointByType.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			User user = getReqUser(req);

			String type = req.getParameter("type");
			check(type);

			User simpleUser = new User();
			simpleUser.setCompanyId(user.getCompanyId());
			simpleUser.setCompanyInfoId(user.getCompanyInfoId());
			user = simpleUser;

			RedPointServicePrx redPointService = ServiceUtils.getRedPointServicePrx();
			List<NewRedPoint> redPoints = redPointService.getRedPointByType(user, type);

			String msg = SuccessMsg.getDefaultMsg(redPoints);

			logger.info("返回json:{}", msg);

			printJson(resp, msg);

		} catch (Throwable e) {
			onThrowable(req, resp, e);
		}
	}

	private void check(String type) {
		if (StringUtils.isEmpty(type)) {
			throw new RpcServerLogicalException(1, "参数错误");
		}
	}
}
